# -*- coding: utf-8 -*-
import os
import cgi
import urllib

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.api import urlfetch
# from newsreader import newsreader
import yasa

class MainPage(webapp.RequestHandler):
  def get(self):
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    template_values = {
      'greetings': 'blah',
      'url': 'blah',
      'url_linktext': 'blah',
      }
    self.response.out.write(template.render(path, template_values))


class DemoPage(webapp.RequestHandler):
  path = os.path.join(os.path.dirname(__file__), 'demo.html')
  def post(self):
    yasaobj = yasa.YASAObj()
    yasaobj.content = self.request.get('inbox')
    yasaobj.minLength = int(self.request.get('min'))
    yasaobj.freqThreshold = int(self.request.get('freq'))

    resultJson = yasaobj.getFreq()
    resultContent = ""
    for x in resultJson:
	resultContent += x["Term"] + "," + x["Freq"] + "\n"

    template_values = {
	'inbox': yasaobj.content,
	'result': resultContent,
	'min': yasaobj.minLength,
	'freq': yasaobj.freqThreshold,
        }
    self.response.out.write(template.render(self.path, template_values))
  def get(self):
    template_values = {
	'greetings': 'as',
	'min': '1',
	'freq': '2'
	}
    self.response.out.write(template.render(self.path, template_values))

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/demo', DemoPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

